package com.cloud.mq.topic;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    public static final String QUEUE_TRANSFER_MOVIE = "queue_transfer_movie";
    public static final String EXCHANGE_TOPICS ="exchange_topics";
    public static final String ROUTINGKEY_TRANSFER_MOVIE="transfer.movie.key";

    /**
     * 声明topic交换机(分发任务场景实用)
     * 解释: 生产者将消息推送到交换机, 交换机与队列之间有个routing-key关联
     *       在推送消息时会标记此消息被路由到哪个队列
     *
     * publish/subscribe 交换机(多人聊天场景实用)
     *  解释: 生产者将消息推送到交换机, 只要关联了交换机的队列均会收到消息
     */
    @Bean(EXCHANGE_TOPICS)
    public Exchange EXCHANGE_TOPICS(){
        // durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS).durable(true).build();
    }


    // 声明队列
    @Bean(QUEUE_TRANSFER_MOVIE)
    public Queue QUEUE_TRANSFER_MOVIE(){
        return new Queue(QUEUE_TRANSFER_MOVIE);
    }


    //用routing-key路由键, 将交换机和队列进行绑定
    @Bean
    public Binding BINDING_ROUTINGKEY_SMS(@Qualifier(QUEUE_TRANSFER_MOVIE) Queue queue,
                                          @Qualifier(EXCHANGE_TOPICS) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_TRANSFER_MOVIE).noargs();
    }

}
